package com.sumainfor.wx.tenant.service.impl;

import com.sumainfor.common.util.JsonResult;
import com.sumainfor.wx.tenant.dao.HoTenantIdentitMapper;
import com.sumainfor.wx.tenant.dao.RdTteMapper;
import com.sumainfor.wx.tenant.service.HoTenantIdentitService;
import com.sumainfor.wx.tenant.service.RdTteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * pc端调用微信租客端
 */
@Service
public class RdTteServiceimpl implements RdTteService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(RdTteServiceimpl.class);

    @Autowired
    private RdTteMapper rdTteMapper;

    /**
     * 根据租客编号查询密保问题
     */
    @Override
    public JsonResult getRdTteList(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (params.get("TenId") == null) {
            return result.putFailed("参数不正确");
        }
        //查询密保问题
        Map<String, Object> map = rdTteMapper.getRdTteList(params);
        Map<String, Object> tenMap = rdTteMapper.getRdTteTenPhone(params);
        if (tenMap != null) {
            //IfCount 0没有设置密保问题 1设置了密保问题
            if (map != null) {
                map.put("TenPhone", tenMap.get("TenPhone"));
                map.put("IfCount", 1);
            } else {
                map = new HashMap<String, Object>();
                map.put("TenPhone", tenMap.get("TenPhone"));
                map.put("IfCount", 0);
            }
            result.put(map);
        } else {
            result.putFailed("服务器繁忙");
        }
        return result;
    }

    /**
     * 根据租客编号查询密保问题答案
     */
    @Override
    public JsonResult getRdTteAnswerList(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (params.get("TenId") == null || params.get("AnswerA") == null || params.get("AnswerB") == null
                || params.get("AnswerC") == null) {
            return result.putFailed("参数不正确");
        }
        Map<String, Object> map = rdTteMapper.getRdTteAnswerList(params);
        if (map != null) {
            int ifResult = 1;//密保问题是否回答正确 0错误 1正确
            if (!map.get("AnswerA").toString().equals(params.get("AnswerA").toString())) {
                ifResult = 0;
            }
            if (!map.get("AnswerB").toString().equals(params.get("AnswerB").toString())) {
                ifResult = 0;
            }
            if (!map.get("AnswerC").toString().equals(params.get("AnswerC").toString())) {
                ifResult = 0;
            }
            result.put(ifResult);
        } else {
            result.putFailed("服务器繁忙");
        }
        return result;
    }

    /**
     * 根据租客手机号查询密保问题
     */
    @Override
    public JsonResult getRdTteTenantList(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (params.get("TenPhone") == null) {
            return result.putFailed("参数不正确");
        }
        Map<String, Object> map = rdTteMapper.getRdTteTenantList(params);
        //IfCount 0没有设置密保问题 1设置了密保问题
        if (map != null) {
            map.put("IfCount", 1);
        } else {
            map = new HashMap<String, Object>();
            map.put("IfCount", 0);
        }
        result.put(map);
        return result;
    }
}
